Penguin crossfilter¶
View a running version of this notebook. | Download this project.
Cross-filtering Palmer Penguins¶
import numpy as np
import pandas as pd
import holoviews as hv
import hvplot.pandas # noqa
hv.extension('bokeh')
In this introduction to building interactive dashboards we will primarily be using 4 libraries:
- Pandas: To load and manipulate the data
- hvPlot: To quickly generate plots using a simple and familiar API
- HoloViews: To link selections between plots easily
- Panel: To build a dashboard we can deploy
Building some plots¶
Let us first load the Palmer penguin dataset (Gorman et al.) which contains measurements about a number of penguin species:
penguins = pd.read_csv('penguins.csv')
penguins
This diagram provides some background about what these measurements mean:

Next we define an explicit colormapping for each species:
colors = {
'Adelie Penguin': '#1f77b4',
'Gentoo penguin': '#ff7f0e',
'Chinstrap penguin': '#2ca02c'
}
Now we can start plotting the data with hvPlot, which provides a familiar API to pandas .plot users but generates interactive plots.
We start with a simple scatter plot of the culmen (think bill) length and depth for each species:
scatter = penguins.hvplot.scatter('Culmen Length (mm)', 'Culmen Depth (mm)', c='Species', cmap=colors, responsive=True, min_height=300)
scatter
Next we generate a histogram of the body mass colored by species:
histogram = penguins.hvplot.hist('Body Mass (g)', by='Species', color=hv.dim('Species').categorize(colors), legend=False, alpha=0.5, responsive=True, min_height=300)
histogram
Next we count the number of individuals of each species and generate a bar plot:
bars = penguins.hvplot.bar('Species', 'Individual ID', c='Species', cmap=colors, responsive=True, min_height=300).aggregate(function=np.count_nonzero)
bars
Finally we generate violin plots of the flipper length of each species split by the sex:
violin = penguins.hvplot.violin('Flipper Length (mm)', by=['Species', 'Sex'], cmap='Category20', responsive=True, min_height=300).opts(split='Sex')
violin
Linking the plots¶
hvPlot let us build interactive plots very quickly but what if we want to gain deeper insights about this data by selecting along one dimension and seeing that selection appear on other plots? Using HoloViews we can easily compose and link these plots:
ls = hv.link_selections.instance()
ls(scatter.opts(show_legend=False) + bars + histogram + violin).cols(2)
Building a dashboard¶
As a final step we will compose these plots into a dashboard using Panel, so as a first step we will load the Palmer penguins logo:
import panel as pn
logo = pn.panel('logo.png', height=60)
logo
Next we define use some functionality on the link_selections object to display the count of currently selected penguins:
def count(selected):
return pn.pane.Markdown(f"## {len(selected)}/{len(penguins)} penguins selected", align='center')
pn.panel(pn.bind(count, ls.selection_param(penguins)))
Now we will compose these two items into a Row which will serve as the header of our dashboard:
welcome = "## Welcome and meet the Palmer penguins!"
penguins_art = pn.panel('./lter_penguins.png', width=250)
credit = "### Artwork by @allison_horst"
instructions = """
Use the box-select and lasso-select tools to select a subset of penguins
and reveal more information about the selected subgroup through the power
of cross-filtering.
"""
license = """
### License
Data are available by CC-0 license in accordance with the Palmer Station LTER Data Policy and the LTER Data Access Policy for Type I data."
"""
art = pn.Column(welcome, penguins_art, credit, instructions, license, sizing_mode='stretch_width')
art
Deploying the dashboard¶
pn.config.raw_css.append("body .bk-root { font-family: Ubuntu !important; }")
material = pn.template.MaterialTemplate(logo='logo.png', title='Palmer Penguins')
ls = hv.link_selections.instance()
header = pn.Row(
pn.layout.HSpacer(),
pn.bind(count, ls.selection_param(penguins)),
pn.Spacer(width=100),
sizing_mode='stretch_width'
)
header
selections = ls(scatter.opts(show_legend=False) + bars.opts(show_legend=False) + histogram + violin).cols(2)
material.header.append(header)
material.sidebar.append(art)
material.main.append(selections)
material.servable();

View a running version of this notebook. | Download this project.